﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using AlertSystem.Business;
using System.Web.UI.HtmlControls;
using Utils;
using System.IO;
using System.Data;
namespace AlertSystem.Web
{
    public partial class Warning_System_Type_AccountManagement : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        private void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDropDownData();
                BindGridData();
            }
        }

        private void BindGridData()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRecord = 0;
                string warning_type = "", system_id = "";
                string[] s = ddlSearchWarningType_SystemID.SelectedValue.Split('|');
                if (s.Length >= 2)
                {
                    warning_type = s[0].Trim(); system_id = s[1].Trim();
                }
                grvData.DataSource = Warning_System_Type_Account.Paging(DB, currentPage, pageSize, ref totalRecord, warning_type, system_id, ddlSearchAccount_Id.SelectedValue);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRecord;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + totalRecord;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        private void BindDropDownData()
        {
            var source = Warning_Type.GetWarning_TypeCustomSource();
            BindDropDownListData(ddlSearchWarningType_SystemID, source, "MappedField", "MappedField", new ListItem(""));
            BindDropDownListData(ddlWarningType_SystemID, source, "MappedField", "MappedField", new ListItem(""));
            var source1 = DB.Warning_Accounts.OrderBy(p => p.Account_Id).ToList();
            BindDropDownListData(ddlAccount_Id, source1, "Account_Id", "Account_Id", new ListItem(""));
            BindDropDownListData(ddlSearchAccount_Id, source1, "Account_Id", "Account_Id", new ListItem(""));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string ID = e.CommandArgument.ToString();
                hdfID.Value = ID;
                switch (e.CommandName)
                {
                    case "Editing":
                        hdfAction.Value = "Editing";
                        string[] s = ID.Split('|');
                        if (s.Length >= 3)
                        {
                            string warningType = s[0].Trim();
                            string system_id = s[1].Trim();
                            string account_id = s[2].Trim();
                            Warning_System_Type_Account editObj = DB.Warning_System_Type_Accounts.FirstOrDefault(p => p.WarningType == warningType && p.System_Id == system_id && p.Account_Id == account_id);
                            if (editObj != null)
                            {
                                ddlWarningType_SystemID.SelectedValue = editObj.WarningType + " | " + editObj.System_Id;
                                ddlAccount_Id.SelectedValue = editObj.Account_Id;
                                RegisterStartUpScript("OpenDialog('dialog1',600);");
                            }
                        }
                        break;
                    case "Deleting":
                        Authorize(Role.Write);
                        string[] s1 = ID.Split('|');
                        if (s1.Length >= 3)
                        {
                            string warningType = s1[0];
                            string system_id = s1[1];
                            string account_id = s1[2];
                            Warning_System_Type_Account deleteObj = DB.Warning_System_Type_Accounts.FirstOrDefault(p => p.WarningType == warningType && p.System_Id == system_id && p.Account_Id == account_id);
                            if (deleteObj != null)
                            {
                                DB.Warning_System_Type_Accounts.DeleteOnSubmit(deleteObj);
                                DB.SubmitChanges();
                                InsertChangeLog("Xóa dữ liệu Warning_System_Type_Account");
                                BindGridData();
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                if (ValidateInfo())
                {
                    Warning_System_Type_Account obj;
                    if (hdfAction.Value == "Editing")
                    {
                        obj = DB.Warning_System_Type_Accounts.FirstOrDefault(p => p.WarningType == hdfID.Value);
                    }
                    else
                    {
                        obj = new Warning_System_Type_Account();
                    }
                    if (obj != null)
                    {
                        //Gán thuộc tính
                        string[] s = ddlWarningType_SystemID.SelectedValue.Split('|');
                        if (s.Length >= 2)
                        {
                            obj.WarningType = s[0].Trim();
                            obj.System_Id = s[1].Trim();
                            obj.Account_Id = ddlAccount_Id.SelectedValue;
                            if (hdfAction.Value == "AddNew")
                            {
                                if (ValidateInfo(obj))
                                {
                                    DB.Warning_System_Type_Accounts.InsertOnSubmit(obj);
                                }
                                else
                                {
                                    RegisterStartUpScript("OpenDialog('dialog1',600);");
                                    return;
                                }
                            }
                            //save
                            DB.SubmitChanges();
                            //Ghi log tương tác
                            InsertChangeLog(string.Format((hdfAction.Value == "Editing" ? "Cập nhật" : "Thêm mới") + " dữ liệu Warning_System_Type_Account, WarningType: {0}", obj.WarningType));
                        }
                    }
                }
                BindGridData();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected override bool ValidateInfo(object entity)
        {
            Warning_System_Type_Account obj1 = (Warning_System_Type_Account)entity;
            Warning_System_Type_Account obj = DB.Warning_System_Type_Accounts.FirstOrDefault(p => string.Compare(p.WarningType, obj1.WarningType, true) == 0
                && string.Compare(p.System_Id, obj1.System_Id, true) == 0 && string.Compare(p.Account_Id, obj1.Account_Id, true) == 0);
            if (obj != null)
            {
                lbError.Text = "WarningType_System_Id_Account_Id đã tồn tại trong hệ thống.";
                return false;
            }
            return base.ValidateInfo(entity);
        }
    }
}